/**
 * 
 */
package com.hanhai.zrb.api.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.hanhai.zrb.api.biz.dao.mybatis.MessageInfoDao;
import com.hanhai.zrb.api.biz.dao.mybatis.MessageReceiveDao;
import com.hanhai.zrb.api.biz.dao.mybatis.UserDao;
import com.hanhai.zrb.api.biz.service.MessageSendService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.model.message.MessageInfo;
import com.hanhai.zrb.model.message.MessageReceive;
import com.hanhai.zrb.model.message.MessageSend;
import com.hanhai.zrb.model.message.enums.MessageBizType;
import com.hanhai.zrb.model.message.enums.MessageType;

/**
 * @author ricky
 *
 */

@Service("messageSendService")
public class MessageSendServiceImpl implements MessageSendService {
	
	@Resource
	private MessageInfoDao messageInfoDao;
	
	@Resource
	private MessageReceiveDao messageReceiverDao;

	@Resource
	private UserService userService;
	
	@Resource
	private UserDao userDao;
	
	
	@Override
	public boolean sendMessage(MessageSend messageSend) {
		//保存消息发送内容
		saveSendInfo(messageSend.getMessageInfo());
		//List<User> list = userDao.findByUid(messageSend.getReceiverUids());
		//组装消息接收modelList
		List<MessageReceive> receive =  createReceive(messageSend);
		//批量保存消息接收modelList
		saveReceive(receive);
		
		return true;
	}
	
	public boolean createMessage(List<Long> receiverUids,String content,MessageType type,MessageBizType bizType){

		if(CollectionUtils.isEmpty(receiverUids)){
			throw new RuntimeException("站内消息的接受人不能为空");
		}
		
		MessageSend messageSend = new MessageSend();
		MessageInfo minfo = new MessageInfo();
		minfo.setType(type.name());
		minfo.setTypeName(type.getTypeName());
		minfo.setBizType(bizType.name());
		minfo.setCreateTime(new Date());
		minfo.setStatus(1);
		
		if(StringUtils.isBlank(content)){
			minfo.setContent(bizType.getContentTemplate());
		}else{
			minfo.setContent(content);
		}
		
		
		//系统消息
		minfo.setSenderUid(888888L);
		minfo.setSenderUname("系统");
		
		
		messageSend.setMessageInfo(minfo);
		messageSend.setReceiverUids(receiverUids);
		
		return this.sendMessage(messageSend);
	}
	
	public boolean createNotifyMessage(List<Long> receiverUids,String content,MessageBizType bizType,String bizKey){
		
		if(CollectionUtils.isEmpty(receiverUids)){
			throw new RuntimeException("站内消息的接受人不能为空");
		}
		
		MessageSend messageSend = new MessageSend();
		MessageInfo minfo = new MessageInfo();
		minfo.setType(MessageType.notify.name());
		minfo.setTypeName(MessageType.notify.getTypeName());
		minfo.setBizKey(bizKey);
		minfo.setBizType(bizType.name());
		
		minfo.setCreateTime(new Date());
		minfo.setStatus(1);
		
		if(StringUtils.isBlank(content)){
			minfo.setContent(bizType.getContentTemplate());
		}else{
			minfo.setContent(content);
		}
		
		
		//系统消息
		minfo.setSenderUid(888888L);
		minfo.setSenderUname("系统");
		
		
		messageSend.setMessageInfo(minfo);
		messageSend.setReceiverUids(receiverUids);
		
		return this.sendMessage(messageSend);
	}
	
	private List<MessageReceive> createReceive(MessageSend messageSend){
		
		Long messageInfoId = messageSend.getMessageInfo().getId();
		Date date = messageSend.getMessageInfo().getCreateTime();
		String type = messageSend.getMessageInfo().getType();
		List<MessageReceive> list = new ArrayList<MessageReceive>();
		
		for(Long uid:messageSend.getReceiverUids()){
			MessageReceive rec = new MessageReceive();
			rec.setReceiverUid(uid);
			rec.setMessageInfoId(messageInfoId);
			rec.setRead(0);
			rec.setStatus(1);
			rec.setType(type);
			rec.setCreateTime(date);
			list.add(rec);
		}
		return list;
	}
	
	

	private void saveSendInfo(MessageInfo messageInfo){
		messageInfoDao.save(messageInfo);
	}

	
	private void saveReceive(List<MessageReceive> receiveList){
		messageReceiverDao.batchSave(receiveList);
	}
}
